package 线程;

public class 死锁 {
    public static void main(String[] args) {
        DeadLock deadLock1 = new DeadLock(true);
        DeadLock deadLock2 = new DeadLock(false);
        deadLock1.start();
        deadLock2.start();


    }
}

class DeadLock extends Thread{
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public DeadLock(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if(flag){
            synchronized (o1){
                System.out.println("进入1");
                synchronized (o2){
                    System.out.println("进入2");
                }
            }
        }else {
            synchronized (o2){
                System.out.println("进入3");
                synchronized (o1){
                    System.out.println("进入4");
                }
            }
        }
    }
}
